<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>系统日志</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="layui-elem-field layuimini-search">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="searchFrm">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="loginName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">请求接口</label>
                            <div class="layui-input-inline">
                                <input type="text" name="requestUrl" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">访问状态</label>
                            <div class="layui-input-inline">
                                <select name="responseStatus" lay-verify="">
                                    <option value="">请选择</option>
                                    <option value="1">正常</option>
                                    <option value="0">异常</option>
                                </select>     
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">异常信息</label>
                            <div class="layui-input-inline">
                                <input type="text" name="responseError" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item" style="margin-top: 5px;">
                            <div class="layui-block" style="text-align: center;">
                                <a class="layui-btn " lay-submit=""  lay-filter="data-search-btn"><label class="layui-icon layui-icon-search"></label>搜索</a>
                                <a class="layui-btn layui-btn-warm" onclick="javascript:$('#searchFrm')[0].reset()" lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>重置</a>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="logToolBar">
		    <a class="layui-btn layui-btn-sm layui-btn-danger data-count-delete" lay-event="delete">删除</a>
		</script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>
<script src="/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/lib/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="/lib/common/common.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        var tableIns = table.render({
            elem: '#currentTableId',
            url: api + '/log/getByPage',
            method: 'post',
            contentType: "application/json; charset=utf-8",
            toolbar:"#logToolBar",
            cellMinWidth: true,
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            },
            response:{ // 必须配置，否则不加载数据
                statusName:'code', //规定返回的状态码字段为code
                statusCode:200 //规定成功的状态码味200
            },
            done: function(res, curr, count){
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                // console.log(res);
                //得到当前页码
                // console.log(curr); 
                //得到数据总量
                // console.log(count);
            },
            cols: [[
                {type: "checkbox", width: 50, fixed: "left"},
                {type: "numbers", width: 40, title: '序号', fixed: "left"},
                {field: 'id', width: 80, title: 'ID', hide: true},
                {field: 'loginName', width: 80, title: '用户名'},
                {field: 'loginIp', width: 100, title: '请求IP'},
                {field: 'requestUrl', width: 150, title: '请求接口'},
                {field: 'requestMethod', width: 100, title: '请求方式'},
                {field: 'requestParams', minWidth: 140, title: '请求参数'},
                {field: 'responseStatus', width: 90, title: '访问状态', templet:function(d){
                        return d.responseStatus==0?'异常':'正常';
                    }},
                {field: 'responseResult', minWidth: 150, title: '返回值'},
                {field: 'responseError', width: 100, title: '异常信息'},
                {field: 'responseTime', width: 100, title: '响应耗时', sort: true},
                {field: 'createTime', width: 200, title: '创建时间', sort: true}
            ]],
            limits: [10, 15, 20, 25, 5],
            limit: 15,
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                },
                where: {
                    queryParams: {
                        "loginName": data.field.loginName,
                        "requestUrl": data.field.requestUrl,
                        "responseStatus": data.field.responseStatus,
                        "responseError": data.field.responseError
                    }
                },
                async: true
            }, 'data');

            return false;
        });

        
		//监听表头的事件
		table.on('toolbar(currentTableFilter)', function(obj){
          var checkStatus = table.checkStatus(obj.config.id);
          console.log(checkStatus);
		  switch(obj.event){
		    case 'delete':
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if(data.length > 0) {
                    layer.confirm('真的删除么', function (index) {
                        var arr = [];
                        //遍历
                        data.forEach(function(item){
                            arr.push(item.id)
                        });
                        $.ajax({
                            url: api + "/log/deleteByIds",
                            method: "delete",
                            async: true,
                            contentType: "application/json; charset=utf-8",
                            data: JSON.stringify(arr),
                            success:function(res){
                                layer.msg(res.msg);
                                tableIns.reload();
                            },
                            error:function(){
                                layer.msg("删除失败");
                            }
                        });
                        layer.close(index);
                    });
                }
		    break;
		  };
		});
    });
</script>
<script>

</script>

</body>
</html>